﻿Imports FinanceiroDAL
Public Class ItensCtasPagarBLL
    Dim objDAL As ItensCtasPagarDAL
#Region "Propriedades"
    Property icpcodigo As Integer
    Property ctpcodigo As Integer
    Property icp_datapagamento As Nullable(Of Date)
    Property icp_valor As Decimal
    Property pag_codigo As Integer
    Property icp_datavencimento As Nullable(Of Date)
    Property icp_valorpago As Decimal
    Property icp_desconto As Decimal
    Property icp_jam As Decimal
    Property icp_parcela As Integer
    Property con_codigo As Integer
    Property icp_boletocheque As String
    Property sta_codigo As Integer
#End Region
#Region "Construtores"
    Sub New(ByVal codigo As Integer, ByVal tipo As Integer)
        carrega(codigo, tipo)
    End Sub
    Sub New()
    End Sub
    Sub New(ByVal icpcod As Integer, ByVal cod As Integer, ByVal datapgto As Nullable(Of Date), ByVal valor As Decimal, ByVal tipopagam As Integer, ByVal dtvencimento As Nullable(Of Date), ByVal vlrPago As Decimal,
            ByVal descont As Decimal, ByVal juros As Decimal, ByVal parc As Integer, ByVal conta As Integer, ByVal bolcheque As String, ByVal stat As Integer)
        Me.icpcodigo = icpcod
        Me.ctpcodigo = cod
        Me.icp_datapagamento = datapgto
        Me.icp_valor = valor
        Me.pag_codigo = tipopagam
        Me.icp_datavencimento = dtvencimento
        Me.icp_valorpago = vlrPago
        Me.icp_desconto = descont
        Me.icp_jam = juros
        Me.icp_parcela = parc
        Me.con_codigo = conta
        Me.icp_boletocheque = bolcheque
        Me.sta_codigo = stat
    End Sub
#End Region
#Region "Construtores"
    Sub carrega(ByVal codigo As Integer, ByVal tip As Integer)
        Dim tab As DataTable
        Try
            objDAL = New ItensCtasPagarDAL
            tab = objDAL.buscar(codigo, tip)   'modificar em DAL
            If (tab.Rows.Count > 0) Then
                Me.icpcodigo = tab.Rows(0).Item("icp_codigo").ToString
                Me.ctpcodigo = tab.Rows(0).Item("ctp_codigo").ToString
                If (tab.Rows(0)("icp_datapagamento") Is DBNull.Value) Then
                    Me.icp_datapagamento = Nothing
                Else
                    Me.icp_datapagamento = tab.Rows(0).Item("icp_datapagamento").ToString
                End If
                Me.icp_valor = Decimal.Parse(tab.Rows(0).Item("icp_valor").ToString)
                Me.pag_codigo = tab.Rows(0).Item("pag_codigo").ToString
                If (tab.Rows(0)("icp_datavencimento") Is DBNull.Value) Then
                    Me.icp_datavencimento = Nothing
                Else
                    Me.icp_datavencimento = tab.Rows(0).Item("icp_datavencimento").ToString
                End If
                Me.icp_valorpago = Decimal.Parse(tab.Rows(0).Item("icp_valorpago").ToString)
                Me.icp_desconto = Decimal.Parse(tab.Rows(0).Item("icp_desconto").ToString)
                Me.icp_jam = Decimal.Parse(tab.Rows(0).Item("icp_jam").ToString)
                Me.icp_parcela = tab.Rows(0).Item("icp_parcela").ToString
                Me.con_codigo = tab.Rows(0).Item("con_codigo").ToString
                Me.icp_boletocheque = tab.Rows(0).Item("icp_boletocheque").ToString
                Me.sta_codigo = tab.Rows(0).Item("sta_codigo").ToString
            End If
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
    End Sub
#End Region
#Region "Metodos"
    Sub limpar()
        Me.icpcodigo = Nothing
        Me.ctpcodigo = Nothing
        Me.icp_datapagamento = Nothing
        Me.icp_valor = Nothing
        Me.pag_codigo = Nothing
        Me.icp_datavencimento = Nothing
        Me.icp_valorpago = Nothing
        Me.icp_desconto = Nothing
        Me.icp_jam = Nothing
        Me.icp_parcela = Nothing
        Me.con_codigo = Nothing
        Me.icp_boletocheque = Nothing
        Me.sta_codigo = Nothing
    End Sub
#End Region
#Region "Inserir"
    Public Function InserirItensCtasPagar() As Boolean
        Dim ret As Boolean
        Try
            If (valida()) Then
                objDAL = New ItensCtasPagarDAL
                objDAL.InserirItensCtasPagar(Me.icpcodigo, Me.ctpcodigo, Me.icp_datapagamento, Me.icp_valor, Me.pag_codigo, Me.icp_datavencimento, Me.icp_valorpago, Me.icp_desconto,
                                            Me.icp_jam, Me.icp_parcela, Me.con_codigo, Me.icp_boletocheque, Me.sta_codigo)
                ret = True
            End If
        Catch ex As Exception
            ret = False
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
    Function updateItensCtasPagar() As Boolean
        Dim ret As Integer
        Try
            If valida() Then
                objDAL = New ItensCtasPagarDAL
                ret = objDAL.updateItensCtasPagar(Me.icpcodigo, Me.ctpcodigo, Me.icp_datapagamento, Me.icp_valor, Me.pag_codigo, Me.icp_datavencimento, Me.icp_valorpago, Me.icp_desconto, Me.icp_jam,
                                                     Me.icp_parcela, Me.con_codigo, Me.icp_boletocheque, Me.sta_codigo)
                ret = True
            End If
        Catch ex As Exception
            ret = False
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function

#End Region
#Region "Localizar"
    Public Function localizar(codigo As String, tipo As Integer) As DataTable
        Try
            objDAL = New ItensCtasPagarDAL
            Return objDAL.buscar(codigo, tipo)
        Catch ex As Exception
            Throw ex
        End Try
    End Function
#End Region
#Region "Excluir"
    Function excluir(ByVal cod As Integer) As Boolean
        Try
            objDAL = New ItensCtasPagarDAL
            objDAL.excluir(cod)
            Return True
        Catch ex As Exception
            Throw ex
            Return False
        End Try
    End Function
#End Region
#Region "Soma Parcelas" '
    Function Somaparcelas() As Decimal
        Dim ret As Decimal
        Try
            objDAL = New ItensCtasPagarDAL
            ret = objDAL.Somaparcelas(Me.ctpcodigo)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
    Function Somactp(ByRef cod As Integer) As Decimal
        Dim ret As Decimal
        Try
            objDAL = New ItensCtasPagarDAL
            ret = objDAL.Somaparcelas(cod)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
    Function Somaparcelatotal(ByRef cod As Integer) As Decimal
        Dim ret As Decimal
        Try
            objDAL = New ItensCtasPagarDAL
            ret = objDAL.Somaparcelatotal(cod)
        Catch ex As Exception
            Throw ex
        Finally
            objDAL = Nothing
        End Try
        Return ret
    End Function
#End Region
#Region "Valida Campos obrigatórios"
    Public Function valida() As Boolean
        If (IsNothing(Me.icp_datavencimento)) Then Throw New Exception("Data inválida")
        If (Me.sta_codigo <= 0) Then Throw New Exception("Preencha o status")
        If (Me.icp_valorpago < 0) Then Throw New Exception("Valor inválido!")
        Return True
    End Function

#End Region
End Class
